package com.lxy.user.controller;

import com.lxy.api.client.EquipmentClient;
import com.lxy.common.domain.dto.MaintenanceScheduleDTO;
import com.lxy.common.domain.dto.MaintenanceUsersDTO;
import com.lxy.common.domain.po.Repair;
import com.lxy.common.domain.vo.AlertsVO;
import com.lxy.common.domain.common.R;
import com.lxy.common.domain.vo.MaintenanceSchedulesVO;
import com.lxy.common.domain.vo.RepairUserVO;
import com.lxy.common.utils.UserContext;
import com.lxy.user.domain.dto.AuditDTO;
import com.lxy.user.domain.dto.RegistrationDTO;
import com.lxy.user.domain.po.DailyMaintenance;
import com.lxy.user.domain.po.User;
import com.lxy.user.domain.vo.FreezeOrUnFreezeUserVO;
import com.lxy.user.domain.vo.UserVO;
import com.lxy.user.enums.Permission;
import com.lxy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@Api(tags = "系统管理员和设备管理员相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminController {

    private final UserService userService;
    private final EquipmentClient equipmentClient;

    @ApiOperation("查看所有待审核账号")
    @GetMapping("/getAllAuUser")
    public R<List<User>> getAllAuUser(){
        //判断用户权限
        Long userId = UserContext.getUser();
        User user = userService.getById(userId);
        if(user.getPermission().equals(Permission.SYSTEM_ADMINISTRATOR)){
            return userService.getAllAuUser();
        }else {
            return R.error("您没有权限进行该操作");
        }
    }

    @ApiOperation("审核账号")
    @PostMapping("/AuditUser")
    public R AuditUser(@RequestBody AuditDTO auditDTO) throws Exception {
        //判断用户权限
        Long userId = UserContext.getUser();
        User user = userService.getById(userId);
        if(user.getPermission().equals(Permission.SYSTEM_ADMINISTRATOR)){
            return userService.AuditUser(auditDTO);
        }else {
            return R.error("您没有权限进行该操作");
        }
    }

    @ApiOperation("设备管理员查看所有维护日志")
    @GetMapping("/getAllDailyMaintenance")
    public R<List<DailyMaintenance>> getAllDailyMaintenance(){
        return userService.getAllDailyMaintenance();
    }

    @ApiOperation("查看所有用户信息（系统管理员）")
    @GetMapping("/getAllUser")
    public R<List<RegistrationDTO>> getAllUser(){
        return userService.getAllUser();
    }

    @ApiOperation("冻结或解冻用户(系统管理员)")
    @PostMapping("/freezeOrUnFreezeUser")
    public R freezeOrUnFreezeUser(@RequestBody FreezeOrUnFreezeUserVO freezeOrUnFreezeUserVO){
        return userService.freezeOrUnFreezeUser(freezeOrUnFreezeUserVO);
    }


    @ApiOperation("查看机器人预警（设备管理员）")
    @GetMapping("/showAlerts")
    public R<List<AlertsVO>> showAlerts(){
        return equipmentClient.showAlerts();
    }

    @ApiOperation("查看所有维修人员（设备管理员）")
    @GetMapping("/showRepaierUser")
    public R<List<UserVO>> showRepaierUser(){
        return userService.showRepaierUser();
    }

    @ApiOperation("指派维修人员查看预警（生成维修单）")
    @GetMapping("/alertUser")
    public R alertUser(@RequestParam("alertsId") Long alertsId,@RequestParam("userId") Long userId ){
        return userService.alertUser(alertsId,userId);
    }

    @ApiOperation("新建维护计划")
    @PostMapping("/addMaintenanceSchedule")
    public R addMaintenanceSchedule(@RequestBody MaintenanceScheduleDTO maintenanceScheduleDTO){
        return userService.addMaintenanceSchedule(maintenanceScheduleDTO);
    }

    @ApiOperation("给维修人员安排每日维修任务")
    @PostMapping("/arrangementMaintenanceSchedule")
    public R arrangementMaintenanceSchedule(@RequestBody MaintenanceUsersDTO maintenanceUsersDTO){
        return userService.arrangementMaintenanceSchedule(maintenanceUsersDTO);
    }

    @ApiOperation("新增维护订单的获取人员接口（排除这个订单已有的人员）")
    @PostMapping("/getRepairUsers")
    public R<List<RepairUserVO>> getRepairUsers(@RequestBody List<Long> ids){
        return userService.getRepairUsers(ids);
    }

    @ApiOperation("查看所有维护订单（设备管理员）")
    @GetMapping("/getAllMaintenanceSchedule")
    public R<List<MaintenanceSchedulesVO>> getAllMaintenanceSchedule(){
        return userService.getAllMaintenanceSchedule();
    }

    @ApiOperation("修改维护计划（设备管理员）")
    @PostMapping("/updateMaintenanceSchedule")
    public R updateMaintenanceSchedule(@RequestBody MaintenanceSchedulesVO maintenanceSchedulesVO){
        return userService.updateMaintenanceSchedule(maintenanceSchedulesVO);
    }

    @ApiOperation("查看所有维修订单")
    @GetMapping("/getAllRepair")
    public R<List<Repair>> getAllRepair(){
        return userService.getAllRepair();
    }

}
